Completed
Push — master ( e4a790...21b62f )
by Yaro
01:29
created

app.js ➔ sendRequest   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 9.6818
cc 1
nc 2
nop 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A app.js ➔ ... ➔ $section.each 0 7 2
B app.js ➔ ... ➔ $.ajax.error 0 24 4
A app.js ➔ ... ➔ $.ajax.success 0 5 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
2
$(document).ready(function(){
3
    $('#documenter_nav').tendina({activeMenu: '.current'});
4
    
5
    var hash = window.location.hash;
6
    var a = $('a[href="' + hash + '"]', '#scroll');
7
    if (a.length) {
8
        $('a[href="'+ hash +'"]', '#scroll')[0].click();
9
    } else {
10
        window.location.hash = $($("a[href^='#']")[0]).attr('href');
11
    }
12
    $('section.method').each(function() {
13
        var waypoint = new Waypoint({
0 ignored issues
show
Unused Code introduced by
The variable waypoint seems to be never used. Consider removing it.
Loading history...
Bug introduced by
The variable Waypoint seems to be never declared. If this is a global, consider adding a /** global: Waypoint */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
14
            element : this,
15
            handler : function() {
16
                var id = this.element.attributes['id'].nodeValue;
17
                //
18
                $('a', '#scroll').removeClass('current');
19
                $('ul', '#scroll').removeClass('open');
20
                var a = $('a[href="#' + id + '"]', '#scroll');
21
                a.addClass('current');
22
    
23
                var ul = a.closest('ul');
24
                if (ul.length) {
25
                    openUl(ul);
26
                }
27
                if (a.next().is('ul')) {
28
                    a.next().addClass('open');
29
                }
30
                history.replaceState(null, null, document.location.pathname + '#' + id);
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
            }
32
        })
33
    });
34
    $("a[href^='#']").on('click', function() {
35
        var a = this; 
36
        console.log($(a).attr('href').replace('.', '\\.').replace(':', '\\:'));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
        $('html, body').animate({
38
            scrollTop: $($(a).attr('href').replace(new RegExp(/\./, 'g'), '\\.').replace(new RegExp(/\:/, 'g'), '\\:')).offset().top 
39
        });
40
        return false;
41
    });
42
});
43
44
function openUl(ul)
45
{
46
    ul.addClass('open');
47
    var ul = ul.parent().closest('ul');
48
    if (ul.length) {
49
        openUl(ul);
50
    }
51
}
52
53
function sendRequest(form) 
54
{
55
    $('#toggle-lang-response').trigger('click');
56
    
57
    var $form = $(form);
58
    var $section = $form.closest('section');
59
    
60
    var $btn = $form.find('button[type="submit"]');
61
    $btn.html($('#preloader-template').html()).attr('disabled', true);
62
    
63
    var headers = {};
64
    $section.find('.headers-form .form-group').not('.except').each(function(key, element) {
65
        var $el = $(element);
66
        if ($el.find('.req-header-active').is(':checked')) {
67
            var header = $el.find('.req-header').val();
68
            headers[header] = $el.find('.req-header-value').val();
69
        }
70
    });
71
    
72
    $.ajax({
73
        url : $section.find('.action-url').val(),
74
        headers: headers,
75
        type : $form.attr('method'),
76
        data : $form.serializeArray(),
77
        success : function(response, status, xhr) {
78
            $btn.text('Send').attr('disabled', false);
79
            $section.find('.method-example-endpoint code.response-content').jsonViewer(response); 
80
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
81
        },
82
        error : function(xhr) {
83
            $btn.text('Send').attr('disabled', false);
84
            var content = xhr.responseText;
85
            if (xhr.statusText && !content) {
86
                $.notify({
87
                    message: xhr.statusText
88
                },{
89
                    type: 'danger'
90
                });
91
            }
92
            if (IsJsonString(content)) {
93
                $section.find('.method-example-endpoint code.response-content').jsonViewer(content); 
94
                return;
95
            }
96
            var $frame = $('<iframe class="supa" style="width:100%; height:350px;">');
97
            $section.find('.method-example-endpoint code.response-content').html($frame);
98
            setTimeout(function() {
99
                var doc = $frame[0].contentWindow.document;
100
                var $body = $('body', doc);
101
                $body.html(content);
102
            }, 1);
103
            
104
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
105
        }
106
    });
107
    
108
    return false;
109
}
110
111
function IsJsonString(str) {
112
    try {
113
        JSON.parse(str);
114
    } catch (e) {
115
        return false;
116
    }
117
    return true;
118
}
119
120
function changeApiUrl(input)
121
{
122
    var $input = $(input);
123
    var $form = $input.closest('form');
124
    var $urlInput = $form.find('.action-url');
125
    var original = $urlInput.data('original');
126
    
127
    $form.find('input').not('.action-url').each(function(key, input) {
128
        if (!input.value) {
129
            return;
130
        }
131
132
        var regexp = new RegExp('{'+ input.name +'}', "g");
133
        original = original.replace(regexp, input.value);
134
    });
135
    
136
    $urlInput.val(original);
137
}
138
139
function changeTab(ident)
140
{
141
    $('.method-tab').hide();
142
    $('.method-tab.'+ ident).show();
143
}
144
145